export default {
    data() {
        return {
            tableData: [],
            type:2,
            page: 1,
            limit: 10,
            count: 0
        }
    },
    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        that.getList();
    },

    //方法
    methods: {
        //请求api
        getList() {
            var that = this;
            that.tableData = [];
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.type =  that.type;
            //请求列表
            that.$http.post(that.adminApi.api_url + "/api/srandtyle/index", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body.list) {
                        for (var i in res.body.list) {
                            that.tableData.push({
                                i: i,
                                id: res.body.list[i].id,
                                sort: res.body.list[i].sort,
                                name: res.body.list[i].name,
                                img: "<img src =" + res.body.list[i].picture + " style='height:100px'/ >",
                                describe: res.body.list[i].describe,
                                illustrate: "<span>" + res.body.list[i].system_manager_id + "<span><br /><span>" + res.body.list[i].system_manager_time + "<span>"
                            });
                        }
                        that.count = res.body.count;
                    }else{
                        that.count = 0;
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        //下一页
        handleCurrentChange: function (currentPage) {
            var that = this;
            that.page = currentPage;
        },

        //添加
        add: function () {
            var that = this;
            that.$router.push({path: '/commodity/add_color'});
        },
        //修改
        edit: function (id) {
            var that = this;
            that.$router.push({path: '/commodity/add_color', query: {id: id}});
        },

        //删除
        deleteColor: function (e) {
            var that = this;
            that.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.doDelete(e);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //执行删除
        doDelete: function (e) {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/api/srandtyle/del", {
                token: that.token,
                id: e,
                type: that.type
            }, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '删除成功' }`
                    });
                    that.getList();
                },
                function (res) {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ res.body.msg }`
                    });
                });
        },
    }
}